using UnityEngine;
using System.Collections;

public class PaddleMove : MonoBehaviour {
	
	public int moveSpeed = 140;  //per second
	public bool isComputer = false;
	Vector3 computerDirection = Vector3.up;
	int rate;
	public Transform paddle;
	public static int bucket = 1;
	int manipulator = 1;
	public AudioSource Death;
	public AudioSource Life;
	
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		
		if(!isComputer) PlayerMove();
		else ComputerMove();
		
		
	}
	
	void ComputerMove()
	{
		if(Difficulty.difficulty == 1) rate = 10;
		else rate = 50;
		
		int reverse = Random.Range(0,1000);
		
		Vector3 newPosition = Vector3.zero;
		newPosition = computerDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = newPosition + transform.position;
		
		if(newPosition.y > 84)
		{
			newPosition.y = 84;
			computerDirection.y *= -1;
		}
		else if(newPosition.y < -82)
		{
			newPosition.y = -82;
			computerDirection.y *= -1;
		}
		
		if(reverse <= rate)
		{
			computerDirection.y *= -1;
		}
		
		transform.position = newPosition;
	}
	
	void PlayerMove()
	{
		float x = transform.position.x;
		
		
		Vector3 moveDirection = Vector3.zero;
		Vector3 newPosition = Vector3.zero;
		
		if(Input.GetKey(KeyCode.UpArrow)) moveDirection.y += 1;
		if(Input.GetKey(KeyCode.DownArrow)) moveDirection.y -= 1;
		
		moveDirection = moveDirection * (moveSpeed * Time.deltaTime);
		
		newPosition = transform.position + moveDirection;
		
		newPosition.y = Mathf.Clamp(newPosition.y, -82, 84);
		
		transform.position = newPosition;
		
		if(Score.lives == 2 && x == -75)
		{
			Destroy(transform.gameObject);
			bucket = 2;
			Death.Play();
		}
		
		if(Score.lives == 1 && x == -100)
		{
			Destroy(transform.gameObject);
			bucket = 3;
			Death.Play();
		}
		
		if(Score.lives == 1 && Score.tempScore > 1000)
		{
			Score.tempScore = 0;
			Vector3 offset = Vector3.zero;
			offset.x = (25);
			Transform newPaddle = Instantiate(paddle, transform.position, transform.rotation) as Transform;
			Vector3 changePosition = newPaddle.position + offset;
			newPaddle.position = changePosition;
			Score.lives++;
			manipulator = 2;
			Life.Play();
		}
		
		if(Score.lives == 2 && Score.tempScore > 1000)
		{
			Score.tempScore = 0;
			Vector3 offset = Vector3.zero;
			offset.x = (25 * manipulator);
			Transform newPaddle = Instantiate(paddle, transform.position, transform.rotation) as Transform;
			Vector3 changePosition = newPaddle.position + offset;
			newPaddle.position = changePosition;
			Score.lives++;
			bucket = 1;
			Life.Play();
		}
		
	}

}
